﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LMS.SMSH.MODEL;
using LMS.SMSH.BLL;
using System.Data.Linq;
using System.Data;

namespace LMS.SMSH.WEB
{
    public partial class AdmAdminManage : System.Web.UI.Page
    {
        int rowNo = 0;//行编号
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindGvAdminList();
                btnRemoveAdmin.Attributes.Add("onclick", "javascript:return confirm('确定删除吗?');");
            }
        }

        /// <summary>
        /// 绑定gvAdminList数据
        /// </summary>
        private void bindGvAdminList()
        {
            var query = from adm in LMSDataContext.GetLMSDB.TB_admin
                        select new
                        {
                            adm.mesAdminId,
                            adm.adminPwd,
                            adm.adminName
                        };
            gvAdminList.DataSource = query;
            gvAdminList.DataKeyNames = new string[] { "mesAdminId" };
            gvAdminList.DataBind();
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtAdminNo.Value.Trim()))
            {
                Int32? result = 1;
                var query = LMSDataContext.GetLMSDB.sp_viewAdminInfo(txtAdminNo.Value.Trim(), ref result);

                gvAdminList.DataSource = query;
                gvAdminList.DataKeyNames = new string[] { "mesAdminId" };
                gvAdminList.DataBind();
            }
            else
            {
                bindGvAdminList();
            }

        }

        protected void btnAddAdmin_Click(object sender, EventArgs e)
        {
            Int32? result = -1;
            LMSDataContext.GetLMSDB.sp_addAdmin(txtAdminNo.Value.Trim(), txtAdminName.Value.Trim(), txtAdminPwd.Text.Trim(), ref result);
            //switch (result)
            //{
            //    case 1:
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('添加管理员成功！');", true);
                    bindGvAdminList();
                //    break;
                //case 0:
                //    this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('该用户已经存在！');", true);
                //    break;
                //default:
                //    this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('程序执行错误！');", true);
                //    break;
            //}
        }

        /// <summary>
        /// 移出管理员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRemoveAdmin_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvAdminList.Rows.Count; i++)
            {
                CheckBox chkItem = (CheckBox)gvAdminList.Rows[i].FindControl("chkItem");
                if (chkItem.Checked)
                {
                    Int32? result = -1;
                    LMSDataContext.GetLMSDB.sp_deleteAdmin(gvAdminList.Rows[i].Cells[2].Text.Trim(), ref result);
                    if (result == 1)
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('移出管理员成功！');", true);
                        bindGvAdminList();
                    }
                    else if (result == 0)
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('该管理员不存在！');", true);
                    }
                }
            }
        }

        /// <summary>
        /// 行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvAdminList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                rowNo++;
                e.Row.Cells[0].Text = (gvAdminList.PageIndex * gvAdminList.PageSize + rowNo).ToString();

                //鼠标悬停变色
                e.Row.Attributes.Add("onmouseover", "oldBG=this.style.backgroundColor;this.style.backgroundColor='#FF1199';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=oldBG;");

            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvAdminList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAdminList.PageIndex = e.NewPageIndex;
            bindGvAdminList();
        }

        /// <summary>
        /// 全选、反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void chkHeader_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow gr in gvAdminList.Rows)
            {
                CheckBox chkItem = (CheckBox)gr.FindControl("chkItem");
                //相互变幻
                chkItem.Checked = !chkItem.Checked;
            }
        }

    }
}